package com.group2.equipment.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.group2.equipment.entity.EquipmentInfo;
import com.group2.equipment.entity.UserIdentificationDTO;
import com.group2.equipment.feign.ActiveMonitoringFeign;
import com.group2.equipment.service.EquipmentInfoService;
import com.group2.equipment.utils.PageRequest;
import com.group2.equipment.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * @author binghan
 * @version 1.0
 * @description: TODO 设备信息控制层
 * @date 2022/6/6 16:50
 */
@RestController
@RequestMapping("/equipmentInfo")
public class EquipmentInfoController {

    @Autowired
    EquipmentInfoService equipmentInfoService;

    @Autowired
    ActiveMonitoringFeign activeMonitoringFeign;

    /**
     * 方法未知异常 熔断降级
     * @return
     */
    public Result<Page<EquipmentInfo>> queryFallBack(@RequestBody PageRequest<EquipmentInfo> req) {
        // 返回结果信息
        Result<Page<EquipmentInfo>> res = new Result<>();
        // 返回数据失败
        res.setError();
        res.setMessage("熔断降级，发生异常");
        res.setData(null);
        return res;
    }

    /**
     * 条件分页查询
     *
     * @param req
     * @return
     */
    @PostMapping("/queryByCondition")
    @SentinelResource(value = "queryPageByCondition",fallback = "queryFallBack")
    public Result<PageInfo<EquipmentInfo>> QueryByCondition(@RequestBody PageRequest<EquipmentInfo> req) {
        // 返回结果信息
        Result<PageInfo<EquipmentInfo>> res = new Result<>();
//        boolean flag = new Random().nextBoolean();
//        while(flag) {
//             int b = 1/0;
//        }
        try {
            // 获取返回数据
            PageInfo<EquipmentInfo> equipmentInfoPage = equipmentInfoService.queryPageByCondition(req);
            // 返回数据信息
            res.setData(equipmentInfoPage);
            //  返回数据成功
            res.setSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            // 返回数据失败
            res.setError();
            res.setData(null);
        }
        return res;
    }

    /**
     * 修改
     * @param equipmentInfo
     * @return
     */
    @PutMapping("/updateEquipmentInfo")
    public  Result  updateEquipmentInfo(@RequestBody EquipmentInfo equipmentInfo){
        // 返回结果信息
        Result res = new Result<>();
        try {
            equipmentInfoService.updateById(equipmentInfo);
            //  返回数据成功
            res.setSuccess();
            res.setData(null);
        } catch (Exception e) {
            e.printStackTrace();
            // 返回数据失败
            res.setError();
            res.setData(null);
        }
        return res;
    }

    /**
     * 新增
     * @param equipmentInfo
     * @return
     */
    @PostMapping("/insertEquipmentInfo")
    public  Result  insertEquipmentInfo(@RequestBody EquipmentInfo equipmentInfo){
        // 返回结果信息
        Result res = new Result<>();

        try {
            equipmentInfoService.save(equipmentInfo);
            //  返回数据成功
            res.setSuccess();
            res.setData(null);
        } catch (Exception e) {
            e.printStackTrace();
            // 返回数据失败
            res.setError();
            res.setData(null);
        }
        return res;
    }

    /**
     * 主键批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteEquimentInfo")
    public  Result  deleteEquimentInfo(@RequestBody List<Integer> ids){
        // 返回结果信息
        Result res = new Result<>();
        try {
            equipmentInfoService.deleteEquimentInfo(ids);
            //  返回数据成功
            res.setSuccess();
            res.setData(null);
        } catch (Exception e) {
            e.printStackTrace();
            // 返回数据失败
            res.setError();
            res.setData(null);
        }
        return res;
    }

    /**
     * 设备进行用户认证 程序开始的地方
     * @param userIdentificationDTO
     * @return
     */
    @PostMapping("/userIdentification")
    public Result userIdentification(@RequestBody UserIdentificationDTO userIdentificationDTO) {
        // 返回结果信息
        Result res = new Result<>();
        try {
//        return activeMonitoringFeign.face(userIdentificationDTO.getUserId(),userIdentificationDTO.getAddressName());
            //  返回数据成功
            res.setSuccess();
            res.setData("成功");
        } catch (Exception e) {
            e.printStackTrace();
            // 返回数据失败
            res.setError();
            res.setData("失败");
        }
        return res;
    }

}
